package com.team2.cyber_eye;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.team2.cyber_eye.ActivityHelpers.AdapterDirections;
import com.team2.cyber_eye.ActivityHelpers.AdapterStore;
import com.team2.cyber_eye.ActivityHelpers.DirectionsJSONParser;
import com.team2.shared.Direction;
import com.team2.shared.MapDirectionInfo;
import com.team2.shared.Store;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MapDirectionList extends Activity  {
	
	JSONObject obj;
	AdapterDirections adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		setContentView(R.layout.activity_direction_list);
		
		Typeface font1 = Typeface.createFromAsset(getAssets(), "CanarsieSlab.ttf");
		
		TextView labelText = (TextView) findViewById(R.id.directionListLabel);
		
		labelText.setTypeface(font1);
		
		ListView list = (ListView)this.findViewById(R.id.directionListView);
		
		ImageButton mapViewBtn = (ImageButton) findViewById(R.id.mapViewButton);
		
		mapViewBtn.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	            
	        	MapDirectionList.this.finish();
	        }
	    });
		
		ImageButton homeBtn = (ImageButton) findViewById(R.id.homeButton);
		
		homeBtn.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	            
	        	Intent intent = new Intent(MapDirectionList.this, Home.class);
	        	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        	startActivity(intent);
	        	
	        }
	    });

		
		MapDirectionInfo direcInfo = null;
		
		try {
			obj = new JSONObject(getIntent().getStringExtra("json"));
			
			DirectionsJSONParser parser = new DirectionsJSONParser();
        	
        	// Starts parsing data
			direcInfo = parser.parseDirectionInfo(obj);
			
			EditText fromAddr = (EditText)this.findViewById(R.id.fromText);
			EditText toAddr = (EditText)this.findViewById(R.id.toText);
			TextView distance = (TextView)this.findViewById(R.id.distanceText);
			TextView timeToDest = (TextView)this.findViewById(R.id.timeText);
			
			fromAddr.setText(direcInfo.getFromStreetAddr());
			toAddr.setText(direcInfo.getToStreetAddr());
			distance.setText(direcInfo.getDistanceToDest());
			timeToDest.setText(direcInfo.getTimeToDest());
			
			//Create direction list
			ArrayList<Direction> directions = parser.parseDirectionList(obj);
			
		    adapter = new AdapterDirections(this, directions);
		    list.setAdapter(adapter);

			
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
	}

}
